<?php
class type_model extends jqxGrid_CI_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    
    function get($_id){
        $where=array('_id'=>$_id);
        $query=$this->db->get_where("_type", $where); 
        return $query->result();
    }
    function gets(){
        $where=array('_delete'=>NULL);
        $query=$this->db
                ->where($where)
                ->get("_type"); 
        return $query->result();
    }
    
    function getByType($type=""){
        $where=array('_delete'=>NULL,"_type"=>$type);
        $query=$this->db
                ->where($where)
                ->get("_type"); 
        return $query->result();
    }
    function insert($params){
        try {
            $this->db->set('_insert', 'NOW()', FALSE);
            $this->db->insert("_type", $params); 
            $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
            if($count==1) return true;
        } catch (Exception $exc) {
            //echo $exc->getTraceAsString();
        }
        return false;
    }
    function delete($_id){
        $this->db->set('_delete', 'NOW()', FALSE);
        $where=array('_id'=>$_id);
        $this->db->where($where);
        $this->db->update("_type"); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return $count;
        return false;
    }
    function retore($_id){
        $this->db->set('_delete', 'NULL', FALSE);
        $where=array('_id'=>$_id);
        $this->db->where($where);
        $this->db->update("_type"); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return $count;
        return false;
    }
    function update($_id,$params){
        $this->db->set('_update', 'NOW()', FALSE);
        $this->db->where('_id', $_id);
        $this->db->update("_type", $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return $count;
        return false;
    }
    function binding($type=null){
        
        $strQuery="
            SELECT SQL_CALC_FOUND_ROWS `_type`.*
            FROM `_type`
            ";
        
        if($type!==null){
            $type=  mysql_escape_string($type);
            $strWhere="
            WHERE  `_type` ='$type'
            ";
        }else{
             $strWhere="WHERE true";
        }
        if($_SESSION["ADP"][CCTRL]["display"]==0){
            $strWhere.=" AND `_delete` IS NULL";
        }elseif($_SESSION["ADP"][CCTRL]["display"]==-1){
            $strWhere.=" AND `_delete` IS NOT NULL";
        }
        $strOrderBy="Order By `_insert` DESC ";
        $strGroupBy="";
        $config=array(
            "strQuery"=>$strQuery,
            "strWhere"=>$strWhere,
            "strOrderBy"=>$strOrderBy,
            "strGroupBy"=>$strGroupBy,
            "usingLimit"=>false,
            "fields"=>array(),
            "datefields"=>array()
        );
        $this->init($config);
        return $this->jqxBinding();
    }
    function binding_category(){
        $strQuery="
            SELECT SQL_CALC_FOUND_ROWS `_type`.*
            FROM `_type`
            ";
        $strWhere="
            WHERE  `_type` ='category'
            ";
        if($_SESSION["ADP"][CCTRL]["display"]==0){
            $strWhere.=" AND `_delete` IS NULL";
        }elseif($_SESSION["ADP"][CCTRL]["display"]==-1){
            $strWhere.=" AND `_delete` IS NOT NULL";
        }
        $strOrderBy="Order By `_insert` DESC ";
        $strGroupBy="";
        $config=array(
            "strQuery"=>$strQuery,
            "strWhere"=>$strWhere,
            "strOrderBy"=>$strOrderBy,
            "strGroupBy"=>$strGroupBy,
            "usingLimit"=>false,
            "fields"=>array(),
            "datefields"=>array()
        );
        $this->init($config);
        return $this->jqxBinding();
    }
}
?>
